package com.ntanh.mealplanner.base;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.ntanh.mealplanner.R;

import java.util.List;

/**
 * Created by TuanAnh on 1/26/2015.
 */
public class CustomDrawerAdapter extends ArrayAdapter<DrawerItem> {

	Context context;
	List<DrawerItem> drawerItemList;
	AccountItem accItem;
	int layoutResID;

	public CustomDrawerAdapter(Context context, int layoutResourceID, List<DrawerItem> listItems, AccountItem accItem) {
		super(context, layoutResourceID, listItems);
		this.context = context;
		this.drawerItemList = listItems;
		this.layoutResID = layoutResourceID;
		this.accItem = accItem;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub

		DrawerItemHolder drawerHolder;
		View view = convertView;

		if (view == null) {
			LayoutInflater inflater = ((Activity) context).getLayoutInflater();
			drawerHolder = new DrawerItemHolder();

			view = inflater.inflate(layoutResID, parent, false);
			drawerHolder.ItemName = (TextView) view.findViewById(R.id.drawer_itemName);
			drawerHolder.icon = (ImageView) view.findViewById(R.id.drawer_icon);
			drawerHolder.title = (TextView) view.findViewById(R.id.drawerTitle);
			drawerHolder.accName = (TextView) view.findViewById(R.id.acc_name);
			drawerHolder.accEmail = (TextView) view.findViewById(R.id.acc_email);
			drawerHolder.accAvatar = (ImageView) view.findViewById(R.id.acc_avatar);
			drawerHolder.headerLayout = (LinearLayout) view.findViewById(R.id.headerLayout);
			drawerHolder.itemLayout = (LinearLayout) view.findViewById(R.id.itemLayout);
			drawerHolder.accountLayout = (RelativeLayout) view.findViewById(R.id.accountLayout);

			view.setTag(drawerHolder);
		} else {
			drawerHolder = (DrawerItemHolder) view.getTag();
		}

		DrawerItem dItem = (DrawerItem) this.drawerItemList.get(position);

		if (dItem.isAccountItem()) {
			drawerHolder.headerLayout.setVisibility(LinearLayout.INVISIBLE);
			drawerHolder.itemLayout.setVisibility(LinearLayout.INVISIBLE);
			drawerHolder.accountLayout.setVisibility(RelativeLayout.VISIBLE);

			drawerHolder.accName.setText(accItem.getName());
			drawerHolder.accEmail.setText(accItem.getEmail());
			drawerHolder.accAvatar.setImageDrawable(view.getResources().getDrawable(accItem.getDrawableResID()));
		} else if (dItem.getTitle() != null) {
			drawerHolder.headerLayout.setVisibility(LinearLayout.VISIBLE);
			drawerHolder.itemLayout.setVisibility(LinearLayout.INVISIBLE);
			drawerHolder.accountLayout.setVisibility(RelativeLayout.INVISIBLE);

			drawerHolder.title.setText(dItem.getTitle());
		} else {
			drawerHolder.headerLayout.setVisibility(LinearLayout.INVISIBLE);
			drawerHolder.accountLayout.setVisibility(RelativeLayout.INVISIBLE);
			drawerHolder.itemLayout.setVisibility(LinearLayout.VISIBLE);

			drawerHolder.icon.setImageDrawable(view.getResources().getDrawable(dItem.getImgResID()));
			drawerHolder.ItemName.setText(dItem.getItemName());
			if(position == drawerItemList.size() - 1 || drawerItemList.get(position + 1).getTitle() != null){
				view.findViewById(R.id.itemBreak).setVisibility(View.INVISIBLE);
			}
		}
		return view;
	}

	private static class DrawerItemHolder {
		TextView ItemName, title, accName, accEmail;
		ImageView icon, accAvatar;
		LinearLayout headerLayout, itemLayout;
		RelativeLayout accountLayout;
	}
}